﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using Edge.Common;

namespace Shop.Web.AppCode
{
    public class WebUtils
    {
        public static string SortTitle(string sortName, string sortOption, FetchOptions fetchOptions)
        {
            var encodeSortExpression = HttpUtility.UrlEncode(fetchOptions.SortExpression);
            var str = String.Format(
                "<a href=\"javascript:SetSortCol('{1}', '{2}');\" style=\"padding: 0 20px 0 5px; background: url({3});background-repeat: no-repeat;background-position:right;\">{0}</a>",
                sortName,
                sortOption,
                (sortOption == encodeSortExpression && fetchOptions.SortDirection == SortDirection.Descending)
                    ? "False"
                    : "True",
                sortOption == encodeSortExpression
                    ? (fetchOptions.SortDirection == SortDirection.Ascending
                           ? "App_Themes/Layout/Images/up.gif"
                           : "App_Themes/Layout/Images/down.gif")
                    : "");

            return str;
        }

        public static object JsonDesirialize(string jsobObject)
        {
#pragma warning disable 618,612
            var serializer = new JavaScriptSerializer();
#pragma warning restore 618,612
            var o = serializer.DeserializeObject(jsobObject);
            return o;
        }

        public static T JsonDesirialize<T>(string jsobObject)
        {
#pragma warning disable 618,612
            var serializer = new JavaScriptSerializer();
#pragma warning restore 618,612
            var o = serializer.Deserialize<T>(jsobObject);
            return o;
        }

        public static string Exception2JSON(Exception ex, bool insertBr)
        {
            return ex.ToString().Replace('\'', '"').Replace("\n", (insertBr ? "<br />" : "")).Replace("\r", "");
        }

        public static void SetExportResponse(byte[] result, string name, string extension)
        {
            var response = HttpContext.Current.Response;
            try
            {
                var filename = name + (!string.IsNullOrEmpty(extension) ? "." + extension : "");
                response.Clear();
                response.ClearHeaders();
                response.ClearContent();
                response.Buffer = true;
                response.AddHeader("Pragma", "public");
                response.AddHeader("Content-Transfer-Encoding", "binary");
                response.Charset = "";
                response.AppendHeader("Content-Length", result.Length.ToString());
                response.AppendHeader("Content-Disposition", string.Format("attachment; filename={0}", filename));
                response.AppendHeader("Content-Type", string.Format("application/x-force-download; name=\"{0}\"", filename));
                response.BinaryWrite(result);
                response.Flush();
                response.Close();
            }
            catch (Exception ex)
            {
                response.ClearContent();
                response.Write("<html><body><pre>" + ex + "</pre></body></html>");
                response.Flush();
                response.Close();
            }


        }

        public static string EscapeString(string source)
        {
            return Microsoft.JScript.GlobalObject.escape(source);
        }
    }
}
